package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.frs.v2.FrsClient;
import com.huaweicloud.sdk.frs.v2.model.*;
import com.huaweicloud.sdk.frs.v2.region.FrsRegion;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FaceSetInfoMapper;
import com.ruoyi.system.domain.FaceSetInfo;
import com.ruoyi.system.service.IFaceSetInfoService;

/**
 * 系统人脸库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class FaceSetInfoServiceImpl implements IFaceSetInfoService 
{

    @Autowired
    private ISysDeptService deptService;

    private static FrsClient client;

    static {

        ICredential auth = new BasicCredentials()
                .withAk("9LCWQGUFXTMAVTQQHXPU")
                .withSk("ixf7b2vxl7y6WA7Lwa7l9wOq8009ZJUtFMSrc2K8");

        client = FrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(FrsRegion.valueOf("cn-north-4"))
                .build();
    }

    @Autowired
    private FaceSetInfoMapper faceSetInfoMapper;

    /**
     * 查询系统人脸库
     * 
     * @param id 系统人脸库主键
     * @return 系统人脸库
     */
    @Override
    public FaceSetInfo selectFaceSetInfoById(Long id)
    {
        return faceSetInfoMapper.selectFaceSetInfoById(id);
    }

    /**
     * 查询系统人脸库列表
     * 
     * @param faceSetInfo 系统人脸库
     * @return 系统人脸库
     */
    @Override
    public List<FaceSetInfo> selectFaceSetInfoList(FaceSetInfo faceSetInfo)
    {
        return faceSetInfoMapper.selectFaceSetInfoList(faceSetInfo);
    }

    /**
     * 新增系统人脸库
     * 
     * @param faceSetInfo 系统人脸库
     * @return 结果
     */
    @Override
    public int insertFaceSetInfo(FaceSetInfo faceSetInfo)
    {
        // 华为sdk 创建人脸库
        CreateFaceSetRequest request = new CreateFaceSetRequest();
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        CreateFaceSetReq body = new CreateFaceSetReq();
        body.setFaceSetCapacity(Integer.valueOf(faceSetInfo.getFaceSetCapacity()));
        Map mapType = JSON.parseObject(faceSetInfo.getExternalFields(), Map.class);
//        body.setExternalFields(mapType);
        body.setFaceSetName(faceSetInfo.getFaceSetName());
        request.setBody(body);
        CreateFaceSetResponse response = client.createFaceSet(request);
        System.out.println(response);

        faceSetInfo.setFaceSetId(response.getFaceSetInfo().getFaceSetId());
        faceSetInfo.setFaceSetName(response.getFaceSetInfo().getFaceSetName());
        faceSetInfo.setFaceNumber(0L);
        faceSetInfo.setFaceSetCapacity("100000");

        // 获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 用户名
        String userName = loginUser.getUsername();
        //部门名称
        Long deptId = loginUser.getDeptId();
        // 通过deptId查询部门名称
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptName = sysDept.getDeptName();
        faceSetInfo.setDeptName(deptName);
        faceSetInfo.setCreateBy(userName);

        return faceSetInfoMapper.insertFaceSetInfo(faceSetInfo);
    }

    /**
     * 修改系统人脸库
     * 
     * @param faceSetInfo 系统人脸库
     * @return 结果
     */
    @Override
    public int updateFaceSetInfo(FaceSetInfo faceSetInfo)
    {
        return faceSetInfoMapper.updateFaceSetInfo(faceSetInfo);
    }

    /**
     * 批量删除系统人脸库
     * 
     * @param ids 需要删除的系统人脸库主键
     * @return 结果
     */
    @Override
    public int deleteFaceSetInfoByIds(Long[] ids)
    {
        return faceSetInfoMapper.deleteFaceSetInfoByIds(ids);
    }

    /**
     * 删除系统人脸库信息
     * 
     * @param id 系统人脸库主键
     * @return 结果
     */
    @Override
    public int deleteFaceSetInfoById(Long id)
    {
        return faceSetInfoMapper.deleteFaceSetInfoById(id);
    }

    @Override
    public FaceSetInfo selectFaceSetInfoByFaceSetId(String faceSetId) {
        return faceSetInfoMapper.selectFaceSetInfoByFaceSetId(faceSetId);
    }

    @Override
    public DeleteFaceSetResponse removeFaceSetFace(String faceSetName) {
        DeleteFaceSetRequest request = new DeleteFaceSetRequest();
        request.setFaceSetName(faceSetName);
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        DeleteFaceSetResponse response = client.deleteFaceSet(request);
        return response;
    }

    @Override
    public ShowFaceSetResponse getFaceSetFace(String faceSetName) {
        ShowFaceSetRequest request = new ShowFaceSetRequest();
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        request.setFaceSetName(faceSetName);
        ShowFaceSetResponse response = client.showFaceSet(request);
        return response;
    }
}
